<?php
/**
* @file ChatService.php
* Copyright (c) Friends Group. All rights reserved.
* $Id$
*
*/

require_once(realpath(dirname(__FILE__)) . '/interfaces/IChatService.php');
require_once(realpath(dirname(__FILE__)) . '/DataService.php');
require_once(realpath(dirname(__FILE__)) . '/UserService.php');
require_once(realpath(dirname(__FILE__)) . '/classes/ChatRoom.php');
require_once(realpath(dirname(__FILE__)) . '/classes/UserInChatRoom.php');


class ChatService implements IChatService 
{

    function __construct()
    {
        $this->repo = new DataService();
    }
    
    /**
     * @ReturnType void
     * @ParamType userId 
     * @ParamType roomName   // La Chuoi ten phong
     * Tạo phòng
     */
    public function createRoom($userId, $roomName) 
    {
        // Tao doi tuong UserService
        $userTemp = new UserService();
        // Kiem Tra Du Lieu
        if(is_string($roomName) != true)
            throw new Exception("Kiểu tham số không hợp lệ. Room không phải là 1 đối tượng String");
        
        $user = $userTemp->findUserById($userId);
        
        // Tao doi tuong newRoom tu tham so
        $newRoom = new ChatRoom($roomName, $userId);
        
        // Tao doi tuong UserInChatRoom
        $newUserInChatRoom = new UserInChatRoom($userId, $roomId);
        
        // Insert doi tuong newRoom vao bang ChatRoom
        $this -> repo -> create($newRoom);
        
        var_dump($newRoom);
        
        // Insert doi tuong newUserInChatRoom vao UserInChatRoom
        $this -> repo -> create($newUserInChatRoom);
        
        // var_dump($newRoom);
        //$this->repo->create($room);
        
    }

    /**
     * @ReturnType void
     * @ParamType roomId 
     * Xóa phòng
     */
    public function deleteRoom($roomId) 
    {
        $room = $this->findRoomById($roomId);
        $this -> repo -> delete($room);
    }
    
    /**
     * @ReturnType classes.ChatRoom
     * @ParamType RoomId 
     * Tìm Room theo RoomID
     */
	public function findRoomById($roomId) 
    {
        $room = $this -> repo -> getFirst("SELECT * FROM ChatRoom WHERE id = '$roomId'", "ChatRoom");
        
        if($room == null)
            throw new Exception("Không tìm thấy Room #" . $roomId);
        
        return $room;
    }
	/**
     * @ReturnType Boolean
     * @ParamType RoomId 
     * Kiem tra RoomId có tồn tại hay không ?
     */

	public function isExistdRoomId($roomId)
    {
        try
		{
            $tmp = $this->findRoomById($roomId);
            return true;
        }
        catch(Exception $e)
        {    
			return false;    
		}
    
    }
	
	/**
     * @ReturnType classes.ChatRoom
     * @ParamType RoomName
     * Tìm Room theo RoomName
     */
	public function findRoomByName($roomName) 
    {
        $room = $this -> repo -> getFirst("SELECT * FROM ChatRoom WHERE roomName = '$roomName'", "ChatRoom");
        
        if($room == null)
            throw new Exception("Không tìm thấy Room #" . $roomName);
        
        return $room;
    }
    
    /**
     * @ReturnType void
     * @ParamType userId 
     * @ParamType room 
     * Thêm user vào phòng
     */
    public function addUserToRoom($userId, $room) 
    {
        $userTemp = new UserService();
        
        // Kiem Tra Du Lieu
        if(get_class($room) != 'ChatRoom')
            throw new Exception("Kiểu tham số không hợp lệ. Room không phải là 1 đối tượng ChatRoom");
        
        $user = $userTemp->findUserById($userId);
        
        //Tao doi tuong newRoom tu tham so
        $roomId = $room->getID();
        
        //Tao doi tuong UserInChatRoom
        $newUserInChatRoom = new UserInChatRoom();
        $newUserInChatRoom->setaccountID($userID);
        $newUserInChatRoom->setchatroomID($roomID);
        
        //Test lop $newUserInChatRoom
        var_dump($newUserInChatRoom);
        
        //Insert doi tuong newUserInChatRoom vao UserInChatRoom
        $this -> repo -> create($newUserInChatRoom);
        
        //	var_dump($newRoom);
        //this->repo->create($room);
        
    }

    /**
     * @ReturnType void
     * @ParamType userId 
     * @ParamType room 
     * Remove user khỏi phòng
     */
    public function removeUserFromRoom($userId, $room) 
    {
        if(get_class($room) != 'ChatRoom')
            throw new Exception("Kiểu tham số không hợp lệ. Room không phải là 1 đối tượng ChatRoom");
        
        $userTemp = new UserService();
        
        $user = $userTemp->findUserById($userId);
        
        // Tao doi tuong newRoom tu tham so
        $roomId = $room->getID();
            
        //Tao doi tuong UserInChatRoom
        $newUserInChatRoom=new UserInChatRoom();
        $newUserInChatRoom->setaccountID($userID);
        $newUserInChatRoom->setchatroomID($roomID);
        
        //Test lop $newUserInChatRoom
        var_dump($newUserInChatRoom);
        
        //Insert doi tuong newUserInChatRoom vao UserInChatRoom
        $this -> repo -> create($newUserInChatRoom);
    }
}

?>
